package com.hucejakie.geoquiz;

import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {

    private static final String TAG = "QuizActivity";
    private static final String KEY_INDEX = "Index";
    private static final String KEY_IsCheater = "IsCheater";

    private static final int REQUEST_CODE_CHEAT=0;

    private Button mTrueButton;
    private Button mFalseButton;
    private ImageButton mNextButton;
    private ImageButton mPrevtButton;
    private Button mCheatButton;
    private TextView mQuestionTextView;

    private Question[] mQustionBank = new Question[]
            {
                    new Question(R.string.question_africa, true),
                    new Question(R.string.question_oceans, true),
                    new Question(R.string.question_mideast, false),
                    new Question(R.string.question_americas, false),
                    new Question(R.string.question_australia, false),
                    new Question(R.string.question_asia, true),

            };
    private int mCurrentIndex = 0;
    private boolean mIsCheater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate(Bundle) called");
        setContentView(R.layout.activity_quiz);


        if (savedInstanceState!=null)
        {
            mCurrentIndex=savedInstanceState.getInt(KEY_INDEX,0);
            mIsCheater=savedInstanceState.getBoolean(KEY_IsCheater,false);
        }
        mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
        mQuestionTextView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                goToNextQuestion();
            }
        });

        mTrueButton = (Button) findViewById(R.id.true_button);


        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(true);
            }
        });

        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(false);
            }
        });


        mNextButton = (ImageButton) findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToNextQuestion();
            }
        });

        mPrevtButton = (ImageButton) findViewById(R.id.prev_button);
        mPrevtButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToPrevQuestion();
            }
        });

        mCheatButton=(Button) findViewById(R.id.cheat_button);

        mCheatButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent i= CheatActivity.newIntent(QuizActivity.this,mQustionBank[mCurrentIndex].isAnswerTrue());
                startActivityForResult(i,REQUEST_CODE_CHEAT);
            }
        });
        setQuestionText();
    }



    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG,"onSaveInstanceState");
        outState.putInt(KEY_INDEX,mCurrentIndex);
        outState.putBoolean(KEY_IsCheater,mIsCheater);

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"onStart() called");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"onStop() called");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"onResume() called");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"onPause() called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy() called");
    }

    private void goToNextQuestion() {
        try {
            mCurrentIndex = (mCurrentIndex + 1) % mQustionBank.length;
            setQuestionText();
        }
        catch (Exception ex)
        {
            Log.e(TAG,ex.getMessage());
        }


    }
    private void goToPrevQuestion() {

        if (mCurrentIndex==0)
            return;

        mCurrentIndex = (mCurrentIndex - 1) % mQustionBank.length;
        setQuestionText();

    }
    private  void setQuestionText()
    {
        int question = mQustionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(Integer.toString(mCurrentIndex+1)+"."+getString(question) );//

    }

    private void checkAnswer(boolean userPressedTrue) {

        boolean answerIsTrue= mQustionBank[mCurrentIndex].isAnswerTrue();

        int messageResId=0;
        if (mIsCheater)
        {
            messageResId=R.string.judgment_toast;
        }else
        if (userPressedTrue== answerIsTrue)
        {
            messageResId=R.string.correct_toast;
        }
        else
        {
            messageResId=R.string.incorrect_toast;
        }
        Toast.makeText(QuizActivity.this, messageResId, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode!=REQUEST_CODE_CHEAT)
            return;
        if (requestCode==REQUEST_CODE_CHEAT)
        {
            if (data==null)
                return;
            mIsCheater=CheatActivity.wasAnswerShown(data);
        }
    }
}
